<template>
	<view class="page-bg">
		<u-navbar :border="false" :placeholder="true">
			<view slot="left">

			</view>
			<view slot="center" class="fb c3 f16">
				消息中心
			</view>
		</u-navbar>
		<scroll-view :scroll-top="scrollTop" :style="{height:scrollerHeight}" scroll-y @scrolltolower="onreachBottom">
			<view class="container">
				<view class="f-s-a bf p20" style="padding-left: 12px; padding-right: 12px;"
					v-for="item,index in messages" :key="index" @click="gotomessage(item)">
					<u-icon name="account-fill" size="44"></u-icon>
					<view class="f1 ml10">
						<view class="c3 f16">系统消息</view>
						<view class="c9 f14">{{item.noticeTitle}}</view>
					</view>
				</view>
			</view>
		</scroll-view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				params: {
					pageNum: 1,
					pageSize: 10,
				},
				messages: [],
				scrollerHeight: 0,
				swiperContentHeight: 0,
				scrollTop: 9999,
				ismore: true,

			}
		},
		onLoad() {
			this.getScollHeight();
			this.requestMessage()
		},
		methods: {
			genInterval(){
				if(this.interval == null){
					this.interval = setInterval(() => {
						this.requestMessage()
					},1000)
				}
			},
			clearInterval(){
				if(this.interval != null){
					clearInterval(this.interval)
					this.interval = null
				}
			},
			onreachBottom() {
				if (this.ismore) {
					this.params.pageNum += 1
					this.requestMessage()
				}
			},
			// 设置scoll高度
			getScollHeight() {
				let that = this
				uni.getSystemInfo({
					success: function(res) {
						let windowHeight = res.windowHeight;
						that.swiperContentHeight = (windowHeight - 100) + 'px';
						that.scrollerHeight = (windowHeight - 50) + 'px'; // 给15px的底部间距然后还要减掉搜索位置高度
					}
				});
			},
			requestMessage() {
				let that = this
				that.request("get", "notice/riderApp/list", uni.getStorageSync('token'), that.params).then(
					res => {
						if (res.code === 200) {
							if (that.params.pageNum == 1) {
								this.ismore = true
								that.messages = []
								that.messages = res.rows
							} else {
								that.messages = [...that.messages, ...res.rows]
							}
							if (res.rows.length < 10) {
								that.ismore = false
							} else {
								that.ismore = true
							}
							this.genInterval();
						}
					})
			},
			gotomessage(item) {
				let data = JSON.stringify(item)
				uni.setStorageSync('message', data)
				uni.navigateTo({
					url: '/pagesA/message-details/message-details'
				})
			},
		},
		onHide() {
			this.clearInterval()
		}
	}
</script>

<style>

</style>